<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { ToastError, ToastSuccess, Toastloading } from '@/utils/public';
  import { getPlayEndTime, getDateArray } from '@/utils'
  import { ElMessageBox } from 'element-plus';
  import redPacketform from './redPacketform.vue';
  import Pagination from "@/components/Pagination/index.vue";
  import xmDatePicker from '@/components/xmDatePicker/index.vue'
  const redPacketformRef = ref();
  const action = reactive({
    total:0,
    list:[] as any[],
    timeObj:getPlayEndTime(getDateArray('ymdhis')),
  })
  const listQuery = reactive({
    page:1,
    limit:20,
    rain_id:'',
  })
  const resetQuery = ()=>{
    Object.assign(listQuery,{
      page:1,
      limit:20,
      rain_id:'',
    })
    getList()
  }
  const handleSerach = ()=>{
    listQuery.page = 1
    getList()
  }
  const handleDel = (row:any = {})=>{
    const { id=0 } = row
    if(!id) return ToastError('id not defint')
    ElMessageBox.confirm(`是否删除红包雨ID：${id}?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(async () => {
        const loading = Toastloading();
        gamesApi.redPacketRainDel({
          rain_id:id,
        }).then(()=>{
          ToastSuccess("删除成功!");
          getList();
        }).catch(()=>{
          loading.close();
        })
        
      })
      .catch(() => { });
  }
  const getList = async()=> {
    const loading = Toastloading()
    const { playTime:date_st, endTime:date_ed } = action.timeObj
    const res = await gamesApi.redPacketRainList({
      ...listQuery,
      date_st,
      date_ed
    })
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data
  }
  const handleform = (row:any = {})=>{
    redPacketformRef.value.openDialog(row)
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">红包雨ID：</div>
          <el-input v-model="listQuery.rain_id" placeholder="请输入红包雨ID"/>
        </div>
        <xmDatePicker v-model:timeval="action.timeObj" />
      </div>
      <div class="labelBox">
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="handleform()">新增</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column width="100" prop="id" label="红包雨ID" />
      <el-table-column width="180" prop="start_time" label="开始时间" />
      <el-table-column width="100" prop="limit_second" label="持续秒数" />
      <el-table-column min-width="100" prop="recharge_diamond_num" label="充值钻石数" />
      <el-table-column min-width="100" prop="all_diamond_num" label="总钻石数" />
      <el-table-column min-width="100" prop="packet_num" label="总个数" />
      <el-table-column min-width="100" prop="min_num" label="每个最小值" />
      <el-table-column min-width="100" prop="max_num" label="每个最大值" />
      <el-table-column min-width="100" prop="admin_name" label="管理员名称" />
      <el-table-column width="180" prop="created_at" label="创建时间" />
      <el-table-column width="180" prop="updated_at" label="更新时间" />
      <el-table-column fixed="right" width="180" label="操作">
        <template #default="scope">
          <el-button @click="handleform(scope.row)" type="primary">修改</el-button>
          <el-button @click="handleDel(scope.row)" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <redPacketform @update-list="getList" ref="redPacketformRef" />
  </div>
</template>